
package hotelroom;

/* import statements */
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.Icon;
import javax.swing.ImageIcon;

/**
 *
 * @author Hendry Yuwono
 * 
 * This is a JPanel class called as "Door"
 * This panel will be executed first (as default)
 * It will contains 6 buttons, each button has the same instructions to pop-up a new frame whenever the button is clicked
 * 
 * 
 */
public class Door extends JPanel {
    /* creating variables */
    private JButton r1;
    private JButton r2;
    private JButton r3;
    private JButton r4;
    private JButton r5;
    private JButton r6;
    private JLabel label;
    private JLabel label1;
    private Timer timer;
    private Image img;
    private Icon locked;
    private int levelNum=1;
    private String rmNum1,rmNum2,rmNum3,rmNum4,rmNum5,rmNum6;
    // Following statement would create object ik and vh
    InsertKey ik = new InsertKey();
    ValueHandler vh = new ValueHandler();
    
    
    /**
     * this is a constructor contains 10 parameters.
     */
    public Door(){
        // set default icon on every button
        locked = new ImageIcon(getClass().getResource("locked.png"));
        
        // set default font
        Font f = new Font("serif", Font.PLAIN, 24);
        
        Toolkit kit = Toolkit.getDefaultToolkit();
        img = kit.getImage(Door.class.getResource("background.png"));
        
        vh.setLevel(levelNum);
        
        
        // create buttons and label
        r1 = new JButton();
        r1.setIcon(locked);
        r1.setFont(f);
        r1.setBackground(Color.LIGHT_GRAY);
        r1.setRequestFocusEnabled(false);
        r2 = new JButton();
        r2.setIcon(locked);
        r2.setFont(f);
        r2.setBackground(Color.LIGHT_GRAY);
        r2.setRequestFocusEnabled(false);
        r3 = new JButton();
        r3.setIcon(locked);
        r3.setFont(f);
        r3.setBackground(Color.LIGHT_GRAY);
        r3.setRequestFocusEnabled(false);
        r4 = new JButton();
        r4.setIcon(locked);
        r4.setFont(f);
        r4.setBackground(Color.LIGHT_GRAY);
        r4.setRequestFocusEnabled(false);
        r5 = new JButton();
        r5.setIcon(locked);
        r5.setFont(f);
        r5.setBackground(Color.GRAY);
        r5.setRequestFocusEnabled(false);
        r6 = new JButton();
        r6.setIcon(locked);
        r6.setFont(f);
        r6.setBackground(Color.GRAY);
        r6.setRequestFocusEnabled(false);
        Font customFont = new Font("Pristina", Font.PLAIN, 75);
        String htlNme = "LexiMoore Hotel";
        label = new JLabel();
        label.setText(htlNme);
        label.setBorder(null);
        label.setBackground(null);
        label.setFont(customFont);
        
        label1 = new JLabel();
        label1.setBorder(null);
        label1.setBackground(null);
        label1.setFont(f);
        
        // add action listener on every button
        r1.addActionListener(new HandlerClass());
        r2.addActionListener(new HandlerClass());
        r3.addActionListener(new HandlerClass());
        r4.addActionListener(new HandlerClass());
        r5.addActionListener(new HandlerClass());
        r6.addActionListener(new HandlerClass());
        
        Toolkit toolkit =  Toolkit.getDefaultToolkit ();
        Dimension dim = toolkit.getScreenSize();
        this.setPreferredSize(new Dimension(dim.width,dim.height-50));
        setLayout(new GridBagLayout());
        //creating an object c
        
        GridBagConstraints c = new GridBagConstraints();
        
        //allocating buttons on the panel 
        c.ipady = dim.height/12;
        c.ipadx = dim.width/8;
        c.gridx = 2;
        c.gridy = 2;
        add(r1,c);
        c.gridx = 2;
        c.gridy = 0;
        add(r2,c);
        c.gridx = 1;
        c.gridy = 2;
        add(r3,c);
        c.gridx = 1;
        c.gridy = 0;
        add(r4,c);
        
        
        c.ipadx = dim.width/4;
        c.gridx = 0;
        c.gridy = 2;
        add(r5,c);
        c.gridx = 0;
        c.gridy = 0;
        add(r6,c);
        
        c.ipady = 80;
        c.gridx = 0;
        c.gridy = 1;
        c.gridwidth = 2;
        add(label,c);
        c.gridx = 1;
        c.gridy = 1;
        add(label1,c);
        
        
        //timer to allow switchable button icon works 
        timer = new Timer(100, new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) {
               if(ik.getStatus()==true){
                   vh.setNewIcon(vh.getButton());
                   ik.setStatus(false);
                    }
               if(vh.getLevel()==1){
                rmNum1 = "Room 101";
                rmNum2 = "Room 102";
                rmNum3 = "Room 103";
                rmNum4 = "Room 104";
                rmNum5 = "Room 105";
                rmNum6 = "Room 106";
                if(vh.getLvlIsChanged()==true)
                {
                    r1.setIcon(locked);
                    r2.setIcon(locked);
                    r3.setIcon(locked);
                    r4.setIcon(locked);
                    r5.setIcon(locked);
                    r6.setIcon(locked);
                    vh.setLvlIsChanged(false);
                }
                    
               }else if(vh.getLevel()==2){
                rmNum1 = "Room 201";
                rmNum2 = "Room 202";
                rmNum3 = "Room 203";
                rmNum4 = "Room 204";
                rmNum5 = "Room 205";
                rmNum6 = "Room 206";
                if(vh.getLvlIsChanged()==true)
                {
                    r1.setIcon(locked);
                    r2.setIcon(locked);
                    r3.setIcon(locked);
                    r4.setIcon(locked);
                    r5.setIcon(locked);
                    r6.setIcon(locked);
                    vh.setLvlIsChanged(false);
                }
               }else if(vh.getLevel()==3){
                rmNum1 = "Room 301";
                rmNum2 = "Room 302";
                rmNum3 = "Room 303";
                rmNum4 = "Room 304";
                rmNum5 = "Room 305";
                rmNum6 = "Room 306";
                if(vh.getLvlIsChanged()==true)
                {
                    r1.setIcon(locked);
                    r2.setIcon(locked);
                    r3.setIcon(locked);
                    r4.setIcon(locked);
                    r5.setIcon(locked);
                    r6.setIcon(locked);
                    vh.setLvlIsChanged(false);
                }
               }else if(vh.getLevel()==4){
                rmNum1 = "Room 401";
                rmNum2 = "Room 402";
                rmNum3 = "Room 403";
                rmNum4 = "Room 404";
                rmNum5 = "Room 405";
                rmNum6 = "Room 406";
                if(vh.getLvlIsChanged()==true)
                {
                    r1.setIcon(locked);
                    r2.setIcon(locked);
                    r3.setIcon(locked);
                    r4.setIcon(locked);
                    r5.setIcon(locked);
                    r6.setIcon(locked);
                    vh.setLvlIsChanged(false);
                }
               }else if(vh.getLevel()==5){
                rmNum1 = "Room 501";
                rmNum2 = "Room 502";
                rmNum3 = "Room 503";
                rmNum4 = "Room 504";
                rmNum5 = "Room 505";
                rmNum6 = "Room 506";
                if(vh.getLvlIsChanged()==true)
                {
                    r1.setIcon(locked);
                    r2.setIcon(locked);
                    r3.setIcon(locked);
                    r4.setIcon(locked);
                    r5.setIcon(locked);
                    r6.setIcon(locked);
                    vh.setLvlIsChanged(false);
                }
               }else if(vh.getLevel()==6){
                rmNum1 = "Room 601";
                rmNum2 = "Room 602";
                rmNum3 = "Room 603";
                rmNum4 = "Room 604";
                rmNum5 = "Room 605";
                rmNum6 = "Room 606";
                if(vh.getLvlIsChanged()==true)
                {
                    r1.setIcon(locked);
                    r2.setIcon(locked);
                    r3.setIcon(locked);
                    r4.setIcon(locked);
                    r5.setIcon(locked);
                    r6.setIcon(locked);
                    vh.setLvlIsChanged(false);
                }
               }
               r1.setText(rmNum1);
               r2.setText(rmNum2);
               r3.setText(rmNum3);
               r4.setText(rmNum4);
               r5.setText(rmNum5);
               r6.setText(rmNum6);
               label1.setText("You are at level "+vh.getLevel());
            }
        });
        timer.start();
    }
    
    
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(img,0,0,this);
    }
    
    // this class implements ActionListener which gives instructions to the buttons each time it is clicked
    class HandlerClass implements ActionListener{
        @Override
            public void actionPerformed (ActionEvent e){
            ValueHandler vh = new ValueHandler();
                if(e.getSource() == r1){
                    ik.key();
                    if(vh.getLevel()==1){
                        ik.setCodeDB("RM1001");
                    }else if(vh.getLevel()==2){
                        ik.setCodeDB("RM2001");
                    }else if(vh.getLevel()==3){
                        ik.setCodeDB("RM3001");
                    }else if(vh.getLevel()==4){
                        ik.setCodeDB("RM4001");
                    }else if(vh.getLevel()==5){
                        ik.setCodeDB("RM5001");
                    }else if(vh.getLevel()==6){
                        ik.setCodeDB("RM6001");
                    }
                    vh.setButton(r1);
                }else if(e.getSource() == r2){
                    ik.key();
                    if(vh.getLevel()==1){
                        ik.setCodeDB("RM1002");
                    }else if(vh.getLevel()==2){
                        ik.setCodeDB("RM2002");
                    }else if(vh.getLevel()==3){
                        ik.setCodeDB("RM3002");
                    }else if(vh.getLevel()==4){
                        ik.setCodeDB("RM4002");
                    }else if(vh.getLevel()==5){
                        ik.setCodeDB("RM5002");
                    }else if(vh.getLevel()==6){
                        ik.setCodeDB("RM6002");
                    }
                    vh.setButton(r2);
                }else if(e.getSource() == r3){
                    ik.key();
                    if(vh.getLevel()==1){
                        ik.setCodeDB("RM1003");
                    }else if(vh.getLevel()==2){
                        ik.setCodeDB("RM2003");
                    }else if(vh.getLevel()==3){
                        ik.setCodeDB("RM3003");
                    }else if(vh.getLevel()==4){
                        ik.setCodeDB("RM4003");
                    }else if(vh.getLevel()==5){
                        ik.setCodeDB("RM5003");
                    }else if(vh.getLevel()==6){
                        ik.setCodeDB("RM6003");
                    }
                    vh.setButton(r3);
                }else if(e.getSource() == r4){
                    ik.key();
                    if(vh.getLevel()==1){
                        ik.setCodeDB("RM1004");
                    }else if(vh.getLevel()==2){
                        ik.setCodeDB("RM2004");
                    }else if(vh.getLevel()==3){
                        ik.setCodeDB("RM3004");
                    }else if(vh.getLevel()==4){
                        ik.setCodeDB("RM4004");
                    }else if(vh.getLevel()==5){
                        ik.setCodeDB("RM5004");
                    }else if(vh.getLevel()==6){
                        ik.setCodeDB("RM6004");
                    }
                    vh.setButton(r4);
                }else if(e.getSource() == r5){
                    ik.key();
                    if(vh.getLevel()==1){
                        ik.setCodeDB("RM1005");
                    }else if(vh.getLevel()==2){
                        ik.setCodeDB("RM2005");
                    }else if(vh.getLevel()==3){
                        ik.setCodeDB("RM3005");
                    }else if(vh.getLevel()==4){
                        ik.setCodeDB("RM4005");
                    }else if(vh.getLevel()==5){
                        ik.setCodeDB("RM5005");
                    }else if(vh.getLevel()==6){
                        ik.setCodeDB("RM6005");
                    }
                    vh.setButton(r5);
                }else if(e.getSource() == r6){
                    ik.key();
                    if(vh.getLevel()==1){
                        ik.setCodeDB("RM1006");
                    }else if(vh.getLevel()==2){
                        ik.setCodeDB("RM2006");
                    }else if(vh.getLevel()==3){
                        ik.setCodeDB("RM3006");
                    }else if(vh.getLevel()==4){
                        ik.setCodeDB("RM4006");
                    }else if(vh.getLevel()==5){
                        ik.setCodeDB("RM5006");
                    }else if(vh.getLevel()==6){
                        ik.setCodeDB("RM6006");
                    }
                    vh.setButton(r6);
                } 
        }
    }
    
    
    
}
